//
//  ST_ReceivedRedPacketAlertController.swift
//  btvideo
//
//  Created by mac on 2020/10/19.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit

class ST_ReceivedRedPacketAlertController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(246)
            make.height.equalTo(336)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        root_view.addSubview(root_background_view)
        root_background_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.equalTo(44)
            make.height.equalTo(44)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(168+15+13)
        }
        
        
        
        root_view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(value_label)
        value_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(61)
            make.height.equalTo(70)
            make.centerX.equalToSuperview()
        }
        
        
        root_view.addSubview(value_unit_label)
        value_unit_label.snp.makeConstraints { (make) in
            make.bottom.equalTo(value_label).offset(-11)
            make.left.equalTo(value_label.snp.right).offset(5)
        }
        
        root_view.addSubview(redpacket_status_label)
        redpacket_status_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(131)
            make.height.equalTo(17)
            make.centerX.equalToSuperview()
        }
        
        root_view.addSubview(redpacket_text_label)
        redpacket_text_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(168)
            make.centerX.equalToSuperview()
        }
        
        
        root_view.addSubview(income_icon_image_view)
        income_icon_image_view.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(86)
        }
        
//        bind_data()
        
    }
    
    fileprivate var finishedSpring = false
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if !finishedSpring{
            finishedSpring = true
            
            UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: .curveEaseInOut, animations: {
        
                    self.root_view.transform = CGAffineTransform.identity
        
                }) { _ in
        
                }
            
        }
        
    }
    
    func bind_data(nickName:String,avatar:String,coinNumber:Float,unitStr:String,statusString:String,redPacketStr:String) {
        
        top_view.avatar_image_view.pin_setImage(from: avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        top_view.nickname_label.text = nickName + "的红包"
        
        
        let isInteger = floor(coinNumber) == coinNumber
        
        if !isInteger{
            value_label.text = String(format: "%.2f", coinNumber)
        }else{
            value_label.text = coinNumber.int.string
        }
        
        value_unit_label.text = unitStr
        
        redpacket_status_label.text = statusString
        
        redpacket_text_label.text = redPacketStr
        
    }
    
    
//    MARK: - Proeprties
    
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.cornerRadius = 10
        control.transform = CGAffineTransform.init(scaleX: 0.3, y: 0.3)
        return control
    }()
    
    fileprivate lazy var root_background_view:ST_FWGradientView = {
       let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(hex: 0xFF4343)!.cgColor,UIColor.init(hex: 0xFF5454)!.cgColor]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
//        control.transform = CGAffineTransform.init(scaleX: 0.3, y: 0.3)
        return control
    }()
    
    
    fileprivate lazy var top_view:ST_ReceivedRedPacketTopView = {
       let control = ST_ReceivedRedPacketTopView()
        return control
    }()
    
    fileprivate lazy var value_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#FFD97F")
        control.font = UIFont.init(name: "DINPro-Black", size: 55)!
        return control
    }()
    
    fileprivate lazy var value_unit_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#FFD97F")
        control.font = UIFont.systemFont(ofSize: 12)
        return control
    }()
    
    fileprivate lazy var redpacket_status_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#FFD97F")
        control.font = UIFont.systemFont(ofSize: 12)
        return control
    }()
    
    
    fileprivate lazy var redpacket_text_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hexString: "#FFD97F")
        control.font = UIFont.systemFont(ofSize: 16)
        control.numberOfLines = 0
        return control
    }()
    
    
    fileprivate lazy var income_icon_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("redPacketAlertIncomeIcon"))
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    
    fileprivate lazy var close_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImageForAllStates(ST_ImageUtils.imageName("redPacketClose_icon")!)
      
        control.addTarget(self, action: #selector(close_action), for: UIControl.Event.touchUpInside)
        return control
    }()
}

extension ST_ReceivedRedPacketAlertController{
    
    @objc fileprivate func close_action(){
        
        self.goback_previous_controller(animated: false)
        
    }
}


//MARK: - Child View

class ST_ReceivedRedPacketTopView: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(26)
            make.left.centerY.equalToSuperview()
        }
        
        self.addSubview(nickname_label)
        nickname_label.snp.makeConstraints { (make) in
            make.left.equalTo(avatar_image_view.snp.right).offset(5)
            make.right.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
        control.cornerRadius = 26/2.0
        control.borderColor = UIColor.init(hex: 0xFFD97F)
        control.borderWidth = 1
          return control
      }()
    
    
    
    lazy var nickname_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#FFD97F")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
}
